import axios from "axios";

const BASEURL = "http://kumanxuan1.f3322.net:8809";

export const fetch = (url = "", data = {}, method = "GET", headers = {}) => {
  // console.log(url, data, method);
  // console.log(localStorage.getItem("token"));
  let token = localStorage.getItem("token");
  if (token) {
    headers = { ...headers, ...{ "x-authorization": token } };
  }
  // console.log(headers);
  return new Promise((resolve, reject) => {
    axios({
      url: BASEURL + url,
      data: data,
      method: method,
      headers: headers,
    })
      .then((res) => {
        if (res.code !== 200) {
          console.log(res.data.msg);
        }
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
